<template>
  <div>
    <el-form label-width="150px">
      <el-form-item :label="$t('old')+$t('password')">
        <el-input v-model="oldPassword" type="password"/>
      </el-form-item>
      <el-form-item :label="$t('new')+$t('password')">
        <el-input v-model="newPassword1" type="password"/>
      </el-form-item>
      <el-form-item :label="$t('confirm')+$t('password')">
        <el-input v-model="newPassword2" type="password"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateUser">{{ $t('save') }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { failureNotice, successNotice } from '@/util/common'
import { updateUser } from '@/api/auth/authUserApi'
import { getUserId } from '@/util/storage'

export default {
  name: 'Password',
  data() {
    return {
      newPassword1: null,
      newPassword2: null,
      oldPassword: null,
      userId: getUserId()
    }
  }, methods: {
    updateUser() {
      if (this.newPassword1 && this.newPassword2 && this.oldPassword) {
        if (this.newPassword1 !== this.newPassword2) {
          failureNotice(this, '新密码不同')
          return false
        }
      }
      updateUser({
        userId: this.userId,
        oldPassword: this.oldPassword,
        password: this.newPassword1
      }).then(res => {
        const { code } = res
        if (code === '200') {
          successNotice(this)
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
